package dictionary

import (
    "context"

    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "iip/dictionary/model"
)

func (s *Dictionary) json_error(c *gin.Context, err string) {
    c.JSON(200, gin.H{
        "code":    500,
        "message": err,
    })
}

func (s *Dictionary) json_ok(c *gin.Context) {
    c.JSON(200, gin.H{
        "code":    200,
        "message": "",
    })
}

func (s *Dictionary) json_data(c *gin.Context, d interface{}) {
    c.JSON(200, gin.H{
        "code":    200,
        "message": "",
        "data":    d,
    })
}

func (s *Dictionary) json_paged_data(c *gin.Context, total int64, page, pagesize int, d interface{}) {
    c.JSON(200, gin.H{
        "code":     200,
        "message":  "",
        "total":    total,
        "page":     page,
        "pagesize": pagesize,
        "data":     d,
    })
}

func (s *Dictionary) ctx() context.Context {
    return context.Background()
}

func (s *Dictionary) gorm() *gorm.DB {

    return s.db.WithContext(context.Background())
}

func (s *Dictionary) GetLocationByID(id int64) (*model.DictLocation, error) {
    var row model.DictLocation
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictLocation{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetBusinessRoleByID(id int64) (*model.DictBusinessRole, error) {
    var row model.DictBusinessRole
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictBusinessRole{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetMaterialByID(id int64) (*model.DictMaterial, error) {
    var row model.DictMaterial
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictMaterial{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetMineByID(id int64) (*model.DictMine, error) {
    var row model.DictMine
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictMine{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetCompanyByID(id int64) (*model.DictCompany, error) {
    var row model.DictCompany
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictCompany{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetFactoryByID(id int64) (*model.DictFactory, error) {
    var row model.DictFactory
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictFactory{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetWarehouseByID(id int64) (*model.DictWarehouse, error) {
    var row model.DictWarehouse
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictWarehouse{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetDriverByID(id int64) (*model.DictDriver, error) {
    var row model.DictDriver
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictDriver{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetTruckByID(id int64) (*model.DictTruck, error) {
    var row model.DictTruck
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictTruck{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetWeighbridgeByID(id int64) (*model.DictWeighbridge, error) {
    var row model.DictWeighbridge
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictWeighbridge{}, err
    }
    return &row, nil
}

func (s *Dictionary) GetExternalProjectByID(id int64) (*model.DictExternalProject, error) {
    var row model.DictExternalProject
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.DictExternalProject{}, err
    }
    return &row, nil
}

func (s *Dictionary) company_full(com *model.DictCompany, loc *model.DictLocation) interface{} {
    rsp := &struct {
        model.DictCompany
        Location *model.DictLocation `json:"location"`
    }{}
    rsp.DictCompany = *com
    rsp.Location = loc
    return rsp
}

func (s *Dictionary) mine_full(mine *model.DictMine, loc *model.DictLocation) interface{} {
    rsp := &struct {
        model.DictMine
        Location model.DictLocation `json:"location"`
    }{}
    rsp.DictMine = *mine
    rsp.Location = *loc
    return rsp
}

func (s *Dictionary) factory_full(fac *model.DictFactory, loc *model.DictLocation) interface{} {
    rsp := &struct {
        model.DictFactory
        Location model.DictLocation `json:"location"`
    }{}
    rsp.DictFactory = *fac
    rsp.Location = *loc
    return rsp
}

func (s *Dictionary) warehouse_full(wh *model.DictWarehouse, loc *model.DictLocation) interface{} {
    rsp := &struct {
        model.DictWarehouse
        Location model.DictLocation `json:"location"`
    }{}
    rsp.DictWarehouse = *wh
    rsp.Location = *loc
    return rsp
}

func (s *Dictionary) driver_full(wh *model.DictDriver, com *model.DictCompany) interface{} {
    rsp := &struct {
        model.DictDriver
        Company model.DictCompany `json:"company"`
    }{}
    rsp.DictDriver = *wh
    rsp.Company = *com
    return rsp
}

func (s *Dictionary) truck_full(wh *model.DictTruck, com *model.DictCompany) interface{} {
    rsp := &struct {
        model.DictTruck
        Company model.DictCompany `json:"company"`
    }{}
    rsp.DictTruck = *wh
    rsp.Company = *com
    return rsp
}

func (s *Dictionary) weighbridge_full(wh *model.DictWeighbridge, loc *model.DictLocation) interface{} {
    rsp := &struct {
        model.DictWeighbridge
        Location model.DictLocation `json:"location"`
    }{}
    rsp.DictWeighbridge = *wh
    rsp.Location = *loc
    return rsp
}

func (s *Dictionary) external_project_full(wh *model.DictExternalProject, loc *model.DictLocation) interface{} {
    rsp := &struct {
        model.DictExternalProject
        Location model.DictLocation `json:"location"`
    }{}
    rsp.DictExternalProject = *wh
    rsp.Location = *loc
    return rsp
}
